package listbyorder.access101_200.test123;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/8 13:28
 */
public class Solution1 {

    public int maxProfit(int[] prices) {
        int ans = 0;
        ans = oneMaxProfit(prices, 0, prices.length - 1);

        for (int i = 1; i < prices.length - 2; i++) {
            int temp = oneMaxProfit(prices, 0, i) + oneMaxProfit(prices, i + 1, prices.length - 1);
            ans = Math.max(temp, ans);
        }
        return ans;
    }

    private int oneMaxProfit(int[] prices, int s, int e) {
        if (e - s < 1) return 0;
        int ans = 0;
        int temp;
        int cur = 0;
        for (int i = s; i < e; i++) {
            temp = prices[i + 1] - prices[i];
            cur = cur > 0 ? cur + temp : temp;
            ans = Math.max(cur, ans);
        }
        return ans;
    }
}
